#!/bin/sh
get_this_script_dir()
{
    cd  `dirname $0` &&  echo `pwd` 
}

the_profile="$HOME/.profile"

mkfile()
{
   if [ ! -f $the_profile ]; then
       touch $the_profile
       echo "-- $the_profile is noe exsit, create it by shell_tool.sh"
   fi
}

#$1, profile name
#$2, key
#$3, value 
#$4, is unique
set_key_in_file()
{
    mkfile
    th_profile=$1
    key=$2
    value=$3
    grep -E -v "^[ *#+]" $the_profile | grep -n "$key"  >/dev/null
    if [ $? -ne 0 ]; then #not found
        printf "export $the_env\n" >> $the_profile
        echo "-- Info: [$the_env] does not exist in file $the_profile,add $the_env"
    else  #already exist,delete older and inster newer
        echo "-- Info: [$the_env] already exist in file $the_profile.because it is unique,changing the value to $value"
        key_line=`grep -n "$key" $the_profile | cut -d ':' -f 1 | tail -n 1`
        sed -i "${key_line}a\export $the_env" $the_profile
        sed -i "${key_line}d" $the_profile
    fi
}

#param:
#    $1:key
#    $2:env value
#    $3:1,is unique.  0,can set multiple times
insert_profile()
{
    mkfile
    the_env="$1=$2"
    #key=`echo $the_env | cut -d '=' -f 1`
    #value=`echo $the_env | cut -d '=' -f 2`
    key=$1
    value=$2
    if [ $3 -eq 1 ]; then  #the key can only defined once
        grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
        if [ $? -ne 0 ]; then
            printf "export $the_env\n" >> $the_profile
            echo "-- Info: [$the_env] does not exist in file $the_profile,add $the_env"
        else  #already exist,delete older and inster newer
            echo "-- Info: [$the_env] already exist in file $the_profile.because it is unique,changing the value to $value"
            key_line=`grep -n -w "$key" $the_profile | cut -d ':' -f 1 | tail -n 1`
            sed -i "${key_line}a\export $the_env" $the_profile
            sed -i "${key_line}d" $the_profile
        fi
    elif [ $3 -eq 0 ]; then  #the env can repeat, is not unique
        the_value=`eval echo -n '$'$key`
        #is empty
        if [ -z "$the_value" ] ; then
            printf "export %s=\$%s:%s\n" "$key" "$key" "$value" >> $the_profile
        else #not empty
            #printf "export $key=$%s:%s\n" "$key" "$value" >> $the_profile
            real_value=`eval echo $value`
            exist_flag=`echo $the_value | awk -v value="$real_value" -F ":" '            {
                for(i=1;i<=NF;++i)
                {
                    if($i == value)                    
                    {
                        printf("exist");
                        exit;
                    }
                }
                printf("not_exist");
            }'`
            if [ "$exist_flag" == "exist" ] ; then
                echo "-- Info: [$value] already exist in $key,doing nothing."
                echo "-- use cmd to look up: env | grep -w $key"
            elif [ "$exist_flag" == "not_exist" ]; then
                grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
                if [ $? -ne 0 ]; then
                    printf "export %s=\$%s:%s\n" "$key" "$key" "$value" >> $the_profile
                    echo "-- Info: [$key] does not exist in file $the_profile,it can repeat,add it"
                else  #already exist in the profile, append new data after line
                    echo "-- Info: [$key] already exist in file $the_profile.because it is not unique,append the value to $value"
                    key_line=`grep -n -w "$key" $the_profile | cut -d ':' -f 1 | tail -n 1`
                    sed -i "${key_line},${key_line}s%$%:$value%" $the_profile
                fi
            fi
        fi
    fi

    source $the_profile
}

vmkdir()
{
    if [ ! -d "$1" ]; then
        mkdir -p "$1"
    fi
}
